package ziv.map.utils;

import ziv.map.activity.OtherPeopleActivity;
import ziv.utils.html.HttpDownloader;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class SqliteDB {

	private DbHelper dbHelper = null;

	/**
	 * 初始化数据库操作类
	 * 
	 * @param context
	 * @param databaseName
	 */
	public SqliteDB(Context context, String databaseName) {
		dbHelper = new DbHelper(context, databaseName);
	}

	public void createDatabase() {
		System.out.println("main-->create database");
		SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
		// 初始化配置文件
		
		sqLiteDatabase.execSQL("delete from TB_OBSERVE");
		
		//填充内容
		// 发送get请求连接,把数据存入手机数据库
		HttpDownloader httpDownloader=new HttpDownloader();
		String response=httpDownloader.download(Constant.observeData+"13488661955");
		if(response==null || "".equals(response)){
			//Log.v("ziv", "if");
			 //
		}else{
			String [] row=response.split("_");
			for(int i=0;i<row.length;i++){
				//Log.v("ziv",i+"**"+row[i]);
				String [] element =row[i].split(",");
				//Log.v("ziv", element.toString());
				sqLiteDatabase.execSQL("insert into TB_OBSERVE(_id,MOBILESELF,MOBILEOBSERVE,RELATION) values("+element[0]+",'',"+element[1]+",'"+element[2]+"')");

			}
			
			
		}
		
		 
		sqLiteDatabase.close();

	}

	public void updateDatabase(View arg0, Context context, String databaseName,
			int version) {
		System.out.println("main-->update database");
		DbHelper dbHelper = new DbHelper(context, databaseName, version);
		SQLiteDatabase sqLiteDatabase = dbHelper.getReadableDatabase();
		sqLiteDatabase.close();

	}

	/**
	 * 插入数据
	 * 
	 * @param tableName
	 * @param contentValues
	 */
	public void insertData(String tableName, ContentValues contentValues) {
		System.out.println("main-->insert");
		// ContentValues contentValues=new ContentValues();
		// contentValues.put("id", 1);
		// try {
		// contentValues.put("name", new String ("站工程".getBytes(),"gb2312"));
		// } catch (UnsupportedEncodingException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

		SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
		sqLiteDatabase.insert(tableName, null, contentValues);
		sqLiteDatabase.close();
	}

	/**
	 * 更新数据
	 * 
	 * @param tableName
	 * @param values
	 * @param where
	 * @param whereValue
	 */
	public void update(String tableName, ContentValues values, String where,
			String[] whereValue) {
		System.out.println("main-->update");

		SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
		// sqllitedatebase.update(tableName, values, "id=?", new String[]{"1"});
		sqLiteDatabase.update(tableName, values, where, whereValue);
		sqLiteDatabase.close();

	}
/**
 * 查询数据
 * @param tableName
 */
	public Cursor queryData(String tableName) {
		System.out.println("main-->delete ");

		SQLiteDatabase sqLiteDatabase = dbHelper.getReadableDatabase();

		Cursor cursor = sqLiteDatabase.query(tableName, new String[] { "_id",
				"MOBILEOBSERVE","RELATION" }, null, null, null, null, null);
		//sqLiteDatabase.close();
//		while (cursor.moveToNext()) {
//			String name = cursor.getString(cursor.getColumnIndex("name"));
//			System.out.println("name--->" + name);
//		}
		
		return cursor;
	}
	public void close(){
		if(dbHelper!=null){
			dbHelper.close();
			dbHelper=null;
		}
	}
/*********************setter 、 getter 方法 ****************************/
	public DbHelper getDbHelper() {
		return dbHelper;
	}

	public void setDbHelper(DbHelper dbHelper) {
		this.dbHelper = dbHelper;
	}

}